//
//  PhoneListCell.swift
//  Phone
//
//  Created by redjun Z on 2019/1/30.
//  Copyright © 2019 network.xiaozhang.com. All rights reserved.
//

import UIKit

class PhoneListCell: UITableViewCell {
    
    @IBOutlet weak var userImg: UIImageView!
    
    @IBOutlet weak var iphoneNumber: UITextField!
    @IBOutlet weak var button: UIButton!
    
    var vc: ViewController? = nil
    //    当前手机号码
    var originalNumber = ""
    override func awakeFromNib() {
        super.awakeFromNib()
        registerNotification()
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    //MARK:监听键盘通知
    func registerNotification(){
        //        UIKeyboardWillShow
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(keyBoardWillShow(_ :)),
                                               name: UIResponder.keyboardWillShowNotification,
                                               object: nil)
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(keyBoardWillHide(_ :)),
                                               name: UIResponder.keyboardWillHideNotification,
                                               object: nil)
    }
    
    //MARK:键盘通知相关操作
    @objc func keyBoardWillShow(_ notification:Notification){
        //        DispatchQueue.main.async {
        //        }
    }
    
    @objc func keyBoardWillHide(_ notification:Notification){
        delay(UInt64(0.5)) {
            if self.iphoneNumber.isEnabled {
                DispatchQueue.main.async {
                    self.iphoneNumber.text = self.originalNumber
                    //            self.originalNumber = ""
                }
                let btn = UIAlertAction(title: "知道了", style: .default, handler: nil)
                let _ = UIAlertExtension.showSystemAlert(title: "提示", message: "手机号更改失败❌，重新修改，修改完成后点击 好了 按钮", alertButtons: [btn], preferredStyle: .alert, callback: nil)
            }
        }
    }
    
    //MARK: -------- 手机号正则表达式
    func checkMobileReg(_ mobile: String) -> Bool {
        let regex = try! NSRegularExpression(pattern: REGEXP_MOBILES,
                                             options: [.caseInsensitive])
        return regex.firstMatch(in: mobile, options: [],
                                range: NSRange(location: 0, length: mobile.utf16.count))?.range.length != nil
    }
    
    //MARK:-- 打电话
    @IBAction func buttonClick(_ sender: UIButton) {
        guard let number = iphoneNumber.text else {
            let btn = UIAlertAction(title: "好", style: .destructive, handler: nil)
            let _ = UIAlertExtension.showSystemAlert(title: "提示", message: "手机号不对，看看照片下面的手机号是不是填错了。", alertButtons: [btn], preferredStyle: .alert){
            }
            return
        }
        if(number.count != 11){
            let btn = UIAlertAction(title: "好", style: .destructive, handler: nil)
            let _ = UIAlertExtension.showSystemAlert(title: "提示", message: "手机号不对，看看照片下面的手机号是不是填错了。", alertButtons: [btn], preferredStyle: .alert){
            }
            return
        }
        UIApplication.shared.openURL(URL(string: "tel://"+"\(number)")!)
    }
    
    //    MARK: --- 删除 联系人
    @IBAction func deleteUser(_ sender: Any) {
        let btn = UIAlertAction(title: "删除", style: .default) { (btn) in
            let array = redLocationPhoneNumber()
            let array1 = array?.filter{$0 != self.iphoneNumber.text!}
            userlistUpdate(list: array1 ?? array!)
            if let t = self.vc {
                t.initData()
            }
        }
        let _ = UIAlertExtension.showSystemAlert(title: "提示", message: "确定要删除这个联系人吗", alertButtons: [btn], preferredStyle: .alert, callback: nil)
    }
    
    //    MARK: --- 修改 联系人 手机号
    @IBAction func updateUser(_ sender: UIButton) {
        if iphoneNumber.isEnabled {
            sender.setTitle("修改", for: .normal)
            updateUseriphone(newNumber: iphoneNumber.text ?? "17521091214", originalNumber: originalNumber)
            //更新 图片
            if let t = self.vc {
                t.initData()
            }
        } else {
            originalNumber = iphoneNumber.text ?? "17521091214"
            sender.setTitle("好了", for: .normal)
        }
        iphoneNumber.isEnabled = !iphoneNumber.isEnabled
    }
}


